import React, { Component } from 'react'
import { Button, Form, InputNumber, Row, Col, message, Spin } from 'antd'

import request from '@/Utils/fecth'
import { address } from '@/Utils/apiConfig'
import { limitDecimals } from '@/Utils/common'

const { setting } = address

export default class LogConfig extends Component {
  state = {
    loading: false,
    btnLoading: false,
  }

  componentDidMount() {
    this.getLogSize()
  }

  getLogSize = async() => {
    this.setState({
      loading: true,
    })
    const { code, data } = await request().get(setting.getLogSize)
    if (code === 200) {
      this.form.setFieldsValue({
        size: data || '',
      })
    }
    this.setState({
      loading: false,
    })
  }

  onFinish = async(values) => {
    this.setState({
      btnLoading: true,
    })
    const { code } = await request().post(setting.getLogSize, { ...values })
    if (code === 200) {
      message.success('保存成功')
      this.getLogSize()
    }
    this.setState({
      btnLoading: false,
    })
  }

  render() {
    const { loading, btnLoading } = this.state
    return <div className="LogConfig tab-pane-box">
      <Spin spinning={loading}>
        <Form onFinish={this.onFinish} ref={c => this.form = c}>
          <Row align="middle" className="mb24">
            <Col>
            日志文件超过
            </Col>
            <Col>
              <Form.Item
                style={{ marginBottom: '0' }}
                name="size"
                rules={[
                  {
                    required: true,
                    message: '请输入文件大小',
                  },
                ]}>
                <InputNumber
                  parser={limitDecimals(2)}
                  formatter={limitDecimals(2)}
                  min={0.01}
                  style={{ margin: '0 8px' }} />
              </Form.Item>
            </Col>
            <Col>
          GB时， 发送告警邮件
            </Col>
          </Row>
          <Form.Item>
            <Button loading={btnLoading} htmlType="submit" type="primary">保存</Button>
          </Form.Item>
        </Form>
      </Spin>

    </div>
  }
}
